import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv, defineConfig } from 'vite'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vue from '@vitejs/plugin-vue'
import path, { resolve } from 'path'
import styleImport from 'vite-plugin-style-import'

import viteAntdTheme, { ThemeEntry, AntdThemeOptions } from 'vite-plugin-antd-theme'
const CWD = process.cwd()

// 环境变量
const BASE_ENV_CONFIG = loadEnv('', CWD)
const DEV_ENV_CONFIG = loadEnv('development', CWD)
const PROD_ENV_CONFIG = loadEnv('production', CWD)
const options: AntdThemeOptions = {
    // 是否提取全副变量，默认false，优先级低于设置themeVariables
    allVariables: true,
    // 以下是antd-theme-generator配置项
    antDir: path.join(__dirname, './node_modules/ant-design-vue'),
    stylesDir: path.join(__dirname, './src'),
    varFile: path.join(__dirname, './src/styles/vars.less'),
    themeVariables: ['@primary-color'],
    outputFilePath: path.join(__dirname, './public/static/color.less'),
    customColorRegexArray: [/^fade\(.*\)$/]
};


export default ({ command, mode }: ConfigEnv): UserConfig => {
    // 环境变量
    const { VITE_BASE_URL, VITE_DROP_CONSOLE, VITE_SOURCE_MAP } = loadEnv(mode, CWD)

    // const isBuild = command === 'build';
    return {
        define: {
            // 'process.env': loadEnv(mode, CWD)
        },
        base: VITE_BASE_URL,
        esbuild: {
            target: 'esnext'
        },
        build: {
            outDir: 'dist',
            sourcemap: false,
            minify: 'terser',
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    drop_console: Boolean(VITE_DROP_CONSOLE),
                    drop_debugger: Boolean(VITE_DROP_CONSOLE)
                },
            },
            brotliSize: false,
        },
        resolve: {
            alias: [
                {
                    find: '@',
                    replacement: resolve(__dirname, './src')
                }
            ]
        },
        plugins: [
            vue(),
            vueJsx({
                // options are passed on to @vue/babel-plugin-jsx
            }),
            viteAntdTheme(options),
            // styleImport({
            //     //按需加载
            //     libs: [
            //         {
            //             libraryName: 'ant-design-vue',
            //             esModule: true,
            //             resolveStyle: (name) => {
            //                 return `ant-design-vue/es/${name}/style/index`
            //             }
            //         }
            //     ]
            // })
        ],
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        // 'primary-color': '#e4393c',
                    },
                    javascriptEnabled: true
                },
                scss: {
                    additionalData: `@import "src/styles/global.scss";`
                },
                postcss: {
                    plugins: [
                        require('postcss-pxtorem')({
                            rootValue: 192,
                            // selectorBlackList: ['weui', 'mu'], // 忽略转换正则匹配项
                            propList: ['*']
                        })
                    ]
                }
            }
        },
        server: {
            host: '0.0.0.0',
            port: 3000,
            proxy: {
                '/ALL': { //命名
                    target: 'http://222.186.130.225:8099',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/ALL/, '/ALL')
                },
                '/app-admin': {
                    target: 'http://129.211.117.59:8081',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/app-admin/, '/app-admin')
                },
                // '/app-admin': {
                //     target: 'http://192.168.1.36:8081',
                //     changeOrigin: true,
                //     rewrite: (path) => path.replace(/^\/app-admin/, '/app-admin')
                // },
            }
        },
        optimizeDeps: {
            include: [
                '@ant-design/icons-vue',
                'ant-design-vue/es/locale/zh_CN',
                'ant-design-vue/es/locale/en_US'
            ],
            exclude: ['vue-demi']
        },

    }
}
